package com.demo.handler;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//@ControllerAdvice
public class MyExceptionHandler {
	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Object globalErrorHandler(HttpServletRequest request, Exception e) throws Exception {
		System.out.println("GlobalExceptionHandler globalErrorHandler()......");
		// 创建返回对象Map并设置属性，会被@ResponseBody注解转换为JSON返回
		Map<String, Object> map = new HashMap<>();
		map.put("code", "404");
		map.put("message", "失败");
		map.put("url", request.getRequestURL().toString());
		request.setAttribute("javax.servlet.error.status_code", 500);
		return map;

	}
}
